﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.IService.SystemAdmin;
using Net5ApiAndEfMySql.PoModel.SystemAdmin;

namespace Net5ApiAndEfMySql.API.Controllers
{
    public class RoleDbManagerController : BaseApiController
    {
        private IRoleDbService _iRoleDbService;
        public RoleDbManagerController(IRoleDbService iRoleDbService)
        {
            _iRoleDbService = iRoleDbService;
        }
        /// <summary>
        /// 分页显示查询
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        
        public ResultModel GetPageList(SearchModel m)
        {
            ResultModel result = new ResultModel();
            if (m == null)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _iRoleDbService.GetPageList(m);

                data.draw = m.draw;
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }

        /// <summary>
        /// 角色的添加操作
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        
        public ResultModel RoleAdd(RoleDbPo role)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(role.role_name))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入角色名";
                return result;
            }
            try
            {

                var resultData = _iRoleDbService.AddRole(role);
                if (resultData)
                {
                    result.code = ResultCode.Ok;
                    result.data = resultData;
                    result.mess = "成功!";
                }
                else
                {
                    result.code = ResultCode.Fail;
                    result.data = resultData;
                    result.mess = "添加失败!";
                }

            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        /// <summary>
        /// 角色的修改操作
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel RoleUpdate(RoleDbPo role)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(role.role_name))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入角色名";
                return result;
            }
            try
            {
                var resultData = _iRoleDbService.UpdateRoleDbPo(role);
                result.code = ResultCode.Ok;
                result.data = resultData;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        /// <summary>
        /// 反填操作
        /// </summary>
        /// <param name="role_id"></param>
        /// <returns></returns>
        public ResultModel GetRoleById(int role_id)
        {
            ResultModel result = new ResultModel();
            if (role_id==null)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _iRoleDbService.GetRoleById(role_id);
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="role_id"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel RoleDelete(int role_id)
        {
            ResultModel result = new ResultModel();
            if (role_id == null)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _iRoleDbService.DeleteRoleDbPo(role_id);
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }

    }
}
